#import "YQPCounponNumberModel.h"

#import "MYDiscountCouponVC.h"
#import "MYDiscountCouponListVC.h"
#import <JXCategoryView/JXCategoryView.h>
@interface MYDiscountCouponVC ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@property (nonatomic, copy) NSArray * titles;

@property (strong, nonatomic) YQPCounponNumberModel *model;

@end

@implementation MYDiscountCouponVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"我的优惠券");
    
    [self getCounponItemNumbers];
}

#pragma mark - 获取优惠券的数量
- (void)getCounponItemNumbers
{
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendGetRequest:kGET_COUPON_SELECT_NUMBER_URL parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (responseObject.code == 0) {
            YQPCounponNumberModel *model = [YQPCounponNumberModel mj_objectWithKeyValues:responseObject.data];
            [self setupCouponNumberViews:model];
        }else{
            kMBMesHUD(responseObject.msg);
        }
        YQHIDELOADINGTIPS
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
    }];
}

-(void)setupView {
    [super setupView];
//    [self setupCouponNumberViews];
}

- (void)setupCouponNumberViews:(YQPCounponNumberModel *)model
{
    self.titles = @[model.unusedCount.intValue > 0 ||!IsStrEmpty(model.unusedCount)?[NSString stringWithFormat:@"%@(%@)",kYQLS(@"未使用"),model.unusedCount]:kYQLS(@"未使用"),model.useCount.intValue > 0?[NSString stringWithFormat:@"%@(%@)",kYQLS(@"使用记录"),model.useCount]:kYQLS(@"使用记录"),model.overdueCount.intValue > 0?[NSString stringWithFormat:@"%@(%@)",kYQLS(@"已过期"),model.overdueCount]:kYQLS(@"已过期")];
       self.categoryView = [[JXCategoryTitleView alloc] init];
       self.categoryView.delegate = self;
       self.categoryView.titles = self.titles;
       self.categoryView.defaultSelectedIndex = 0;
       //    self.categoryView.cellSpacing = 82;
       self.categoryView.titleSelectedColor = kAPPThemeColor;//HexColor(0x79C4C7);
       self.categoryView.titleColor = HexColor(0xA0A0A0);
       self.categoryView.titleFont = kSystemMedium(15);
       self.categoryView.titleSelectedFont = kSystemMedium(15);
       self.categoryView.contentEdgeInsetLeft = 12;
       self.categoryView.contentEdgeInsetRight = 12;
       self.categoryView.backgroundColor = UIColor.whiteColor;
       self.categoryView.titleColorGradientEnabled = YES;
       
       self.categoryView.averageCellSpacingEnabled = YES;
       
       JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
       
       lineView.indicatorColor = kAPPThemeColor;
       lineView.indicatorWidth = 32;
       lineView.indicatorHeight = 2.0;
       lineView.verticalMargin = 2;
       self.categoryView.indicators = @[lineView];
       
       [self.view addSubview:self.categoryView];
       
       self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
       self.listContainerView.defaultSelectedIndex = 0;
       [self.view addSubview:self.listContainerView];
       
       self.categoryView.contentScrollView = self.listContainerView.scrollView;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    
    self.categoryView.layer.shadowColor = HexColorA(0x000000, 0.6).CGColor;
    //2.设置阴影偏移范围
    self.categoryView.layer.shadowOffset = CGSizeMake(0, 2);
    //3.设置阴影颜色的透明度
    self.categoryView.layer.shadowOpacity = 0.07;
    
    //4.设置阴影半径
    self.categoryView.layer.shadowRadius = 4;
    //5.设置阴影路径
    self.categoryView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.categoryView.bounds].CGPath;
    
    self.listContainerView.frame = CGRectMake(0, 48, self.view.bounds.size.width, self.view.bounds.size.height - 48);
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}
#pragma mark - JXCategoryViewDelegate end
#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    MYDiscountCouponListVC *listVC = [[MYDiscountCouponListVC alloc] init];
    [self addChildViewController:listVC];
    listVC.type = index;
    return listVC;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

#pragma mark - JXCategoryListContainerViewDelegate end

@end
